#include"stdafx.h"
#include"QQGroupZYZ.h"
#include"accountZYZ.h"
#include"QQMasterAccount.h"
extern accountZYZ currAccount;
extern chatGroupZYZ* currGroup;

void QQGroupZYZ::write2File()
{
	std::string path = SERVICE_DIR + QQGroupZYZ::serviceName + 
		"/groups/" + std::to_string(this->groupID);
	std::ofstream file;
	file.open(path+INFO_FILE);
	if (!file.is_open())
	{
		_mkdir(path.c_str());
		file.open(path +INFO_FILE);
	}
	file << "name: " << this->groupName<<std::endl ;
	file << "ID: " << this->groupID << std::endl;
	file << "Master: " << this->masterID << std::endl;

	file << "admins: " <<adminIDs.size()<< std::endl;
	for (auto i : this->adminIDs)
	{
		file << i << std::endl;
	}
	file << "members: " << memberIDs.size()<< std::endl;
	for (auto i : this->memberIDs)
	{
		file << i << std::endl;
	}
	file.close();
}

bool QQGroupZYZ::exists(int id)
{
	if (id == masterID)
		return true;
	if (memberIDs.count(id))
		return true;
	for (auto i : adminIDs)
	{
		if (i == id)
			return true;
	}

	return false;
}

accountStateZYZ* QQGroupZYZ::getState()
{
	int permisson = this->getPermissonLevel(currAccount.accountID);
	switch (permisson)
	{
	case NORMAL:
		return new QQNormalAccountZYZ();
	case ADMIN:
		return new QQAdminAccount();
	case MASTER:
		return new QQMasterAccount();
	default:
		return nullptr;
	}
}


bool QQGroupZYZ::findGroup(int ID)
{

	std::ifstream file;
	file.open(SERVICE_DIR + QQGroupZYZ::serviceName + 
		"/groups/" + 
		std::to_string(ID)+
		"/info.txt");
	if (file.is_open())
	{
		file.close();
		return true;
	}
	else
		return false;
}

QQGroupZYZ* QQGroupZYZ::getGroup(int ID)
{
		if (QQGroupZYZ::findGroup(ID))
		{
			std::ifstream file;
			file.open(SERVICE_DIR + QQGroupZYZ::serviceName + 
				"/groups/" + std::to_string(ID) + INFO_FILE);
			if (!file.is_open())
				throw("???");
			std::string gName,tmp;
			int gID,masterID,cnt,id;
			std::set<int> adminID;
			std::set<int> memID;

			file >> tmp >> gName;
			file >> tmp >> gID;
			file >> tmp >> masterID;
			file >> tmp >> cnt;
			for (int i = 0; i < cnt; i++)
			{
				file >> id;
				adminID.insert(id);
			}
			file >> tmp >> cnt;
			for (int i = 0; i < cnt; i++)
			{
				file >> id;
				memID.insert(id);
			}
			file.close();
			return new QQGroupZYZ(gID, gName, masterID, adminID, memID);
		}
		else
			throw("groupNotFound Exception");
}

 QQGroupZYZ::QQGroupZYZ(const QQGroupZYZ& source)
 {
	 this->adminIDs = source.adminIDs;
	 this->groupID = source.groupID;
	 this->groupName = source.groupName;
	 this->masterID = source.masterID;
	 this->memberIDs = source.memberIDs;
 }

 QQGroupZYZ::QQGroupZYZ(int gID, 
	 std::string gName, 
	 int masterID, 
	 std::set<int> adminID, 
	 std::set<int> memberID)
{
	this->groupID = gID;
	this->groupName = gName;
	this->masterID = masterID;
	this->adminIDs = adminID;
	this->memberIDs = memberID;
}

 int QQGroupZYZ::getPermissonLevel(int id)
 {
	 if (masterID == id)
		 return MASTER;
	 for (auto i : adminIDs)
	 {
		 if (id == i)
		 {
			 return ADMIN;
		 }
	 }
	 if(memberIDs.count(id))
		return NORMAL;
	
	 throw("???");
 }
